<html>
<head>
<meta charset="UTF-8">
<META HTTP-EQUIV="pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Cache-Control" CONTENT= "no-cache, must-revalidate"> 
<META HTTP-EQUIV="expires" CONTENT= "Wed, 26 Feb 1997 08:21:57 GMT"> 
<link rel="stylesheet" href="css/jquery.dialogbox.css">
<link rel="stylesheet" href="jquery/easyui/themes/default/easyui.css">
<link rel="stylesheet" href="jquery/easyui/themes/icon.css">
</head>
<body>
	<div style="text-align: center;padding: 2px">
		<a href="#" id='applySubmit' onclick="page_orderApplyAudit.applySubmitBase64()" class="easyui-linkbutton" data-options="iconCls:'icon-save'">订单修改提交</a>
	</div>
    <div class="easyui-layout" style="width:100%;height:96%;">
        <div data-options="region:'center',iconCls:'icon-edit'" style="">
            <div class="easyui-layout" data-options="fit:true">
                <div data-options="region:'west',split:true,collapsible:true,hideCollapsedContent:false" title='原订单-对比' style="width:50%">
                	<div id="sourceOrderInfo" class="easyui-panel"  style="width:100%;;height:100%;"
				            data-options="border:false">
				    </div>
                </div>
                <div data-options="region:'center'">
					<div id="applyOrderInfo" class="easyui-panel" title="订单编辑-请在本页修改信息" style="width:100%;height:100%;;"
				            data-options="border:false">
				    </div>
				</div>
                <div data-options="region:'south',split:true,collapsible:true,hideCollapsedContent:false" title='备注信息' style="height:150px">
                	<div class="easyui-accordion" halign="left" style="width:100%;height:100%;">
	                	<div title="备注" contenteditable="true" titleDirection="up" id='myremark' style="font-size:15px;overflow:auto;padding:10px;">
				        </div>
				        
				        <div title="历史备注记录" contenteditable="true" titleDirection="up" id='myremarklist' style="font-size:15px;overflow:auto;padding:10px;">
				        </div>
				        
				        <div title="历史系统记录" contenteditable="true" titleDirection="up" id='myremarksyslist' style="font-size:15px;overflow:auto;padding:10px;">
				        </div>
                	</div>
                </div>
	            </div>
	        </div>
      </div>
      <div id="easyui_dialog_apply"></div>
	<script src="https://upload.shinehao.com.cn/20180816150739jquery-1.8.3.min.js"></script>
	<script src="https://upload.shinehao.com.cn/20180816145716jquery.easyui.min.js"></script>
	<script src="https://upload.shinehao.com.cn/20180816151727easyui-lang-zh_CN.js"></script>
	<script src="js/Util.js"></script>
	<script src="https://upload.shinehao.com.cn/20180816151628jquery.dialogBox.js"></script>
	<script src="js/base.js"></script>
<script>

var	page_orderApplyAudit={
	order_no:"",
	template_id:"",
	apply_id:"",
	order_p_product_no:"",
	init:function(){
		this.order_no=getQueryString("order_no");
		this.order_p_product_no=decodeURI(getQueryString("order_p_product_no"));
		this.template_id=getQueryString("template_id");
		this.getTemplateInfoForSource();
		this.getOrderApplyId();
		this.getRemarkData(this.order_no);
	},
	getRemarkData:function(order_no){//获取备注信息
		$.ajax({
	          type: 'POST',
	          url: serverUrl+"/createOrderApply/getRemarkData",
	          data: {"order_no":order_no},
	          success: function(data){
	            var myremarklist="";
	            var myspecialremarklist='';
	            if(data.data.length>0){
	            	for(i=0;i<data.data.length;i++){
	            		myremarklist+=
	            		"系统编号["+data.data[i].order_no+"]由["+data.data[i].apply_user+"]于["+data.data[i].apply_time+"]"+
	            		"备注为["+data.data[i].apply_remark+"]"+
	            		"提交申请"+
	            		"由["+data.data[i].apply_user+"]于["+data.data[i].audit_time+"]"+
	            		"备注为["+data.data[i].audit_remark+"]"+
	            		"审核结果为["+data.data[i].status+"]"+"</br>"
	            		myspecialremarklist+="申请编号["+data.data[i].apply_id+"]"+"修改内容为["+data.data[i].audit_special_remark+"]</br>"
	            	}
	            	$("#myremarklist").append(myremarklist);
	            	$("#myremarksyslist").append(myspecialremarklist);
	            }
	          }
		});
	},
	getOrderApplyId:function(){
		$.ajax({
	          type: 'POST',
	          url: serverUrl+"/createOrderApply/getOrderApplyId",
	          data: {},
	          success: function(data){
	            if(data.data.result.indexOf("Apply")>-1){
	            	page_orderApplyAudit.apply_id=data.data.result;
	            }else{
	            	window.location.reload();
	            }
	          }
		});
	},
	//获取订单元数据
	getTemplateInfoForSource:function(){
		localStorage.setItem("template_next",0);
		if(page_orderApplyAudit.template_id==0){
			alert("该订单没有模版,不可编辑");
			return;
		}
		$.ajax({
	          type: 'POST',
	          url: serverUrl+"/createOrder/getTemplateById",
	          headers: {
	              Accept: "application/json; charset=utf-8"
	          },
	          data: {"id":page_orderApplyAudit.template_id},
	          success: function(data){
	            console.info(data);
	            localStorage.setItem("temp_info_select",data.data.temp_info);
	            localStorage.setItem("temp_info_table_source_arr",data.data.table_source_arr);
	            localStorage.setItem("temp_info_table_source_arr_str",data.data.table_source_arr_str);
	            localStorage.setItem("table_source_line_total",data.data.table_source_line_total);
	            localStorage.setItem("temp_info_table_source_arr_for_two",data.data.table_source_arr_for_two);
	            localStorage.setItem("temp_info_table_source_arr_str_for_two",data.data.table_source_arr_str_for_two);
	            localStorage.setItem("table_source_line_total_for_two",data.data.table_source_line_total_for_two);
	            localStorage.setItem("template_id",page_orderApplyAudit.template_id);
	            var iframe_ = '<iframe src="template.html?order_no='+page_orderApplyAudit.order_no+'" frameborder="0" style="border:0;width:100%;height:100%;"></iframe>';
	            $('#sourceOrderInfo').panel({
	            	content: iframe_
	            });
	            var timetask = setInterval(() => {
	            	if(localStorage.getItem("template_next")==1){
	            		localStorage.clear();
	            		page_orderApplyAudit.getTemplateInfoForApply();
	            		localStorage.setItem("template_next",0);
	            		clearInterval(timetask);
	            	}
				}, 100);
	           }
	       });
	},//获取订单修改数据  在申请页面 和原数据是保持一致的
	getTemplateInfoForApply:function(){
		if(page_orderApplyAudit.template_id==0){
			alert("该订单没有模版,不可编辑");
			return;
		}
		$.ajax({
	          type: 'POST',
	          url: serverUrl+"/createOrder/getTemplateById",
	          headers: {
	              Accept: "application/json; charset=utf-8"
	          },
	          data: {"id":page_orderApplyAudit.template_id},
	          success: function(data){
	            console.info(data);
	            localStorage.setItem("temp_info_select",data.data.temp_info);
	            localStorage.setItem("temp_info_table_source_arr",data.data.table_source_arr);
	            localStorage.setItem("temp_info_table_source_arr_str",data.data.table_source_arr_str);
	            localStorage.setItem("table_source_line_total",data.data.table_source_line_total);
	            localStorage.setItem("temp_info_table_source_arr_for_two",data.data.table_source_arr_for_two);
	            localStorage.setItem("temp_info_table_source_arr_str_for_two",data.data.table_source_arr_str_for_two);
	            localStorage.setItem("table_source_line_total_for_two",data.data.table_source_line_total_for_two);
	            localStorage.setItem("template_id",page_orderApplyAudit.template_id);
	            var iframe_ = '<iframe src="template.html?order_no='+page_orderApplyAudit.order_no+'" frameborder="0" style="border:0;width:100%;height:100%;"></iframe>';
	            $('#applyOrderInfo').panel({
	            	content: iframe_
	            }); 
	            page_orderApplyAudit.scrollEvent();
	          }
	       });
	},scrollEvent:function(){
		setTimeout(() => {
			$("iframe").contents().scroll(function(event){
				$("iframe").contents().scrollTop($(this).scrollTop());
				$("iframe").contents().scrollLeft($(this).scrollLeft()) ;
				page_orderApplyAudit.functionForbidden();
			});
			page_orderApplyAudit.functionForbidden();
			page_orderApplyAudit.initDialog();
		}, 1000);
	},
	functionForbidden:function(){//针对不同的模式方法禁用
		$("#sourceOrderInfo").find("iframe").contents().find("#printArea").siblings().hide();
		if($("#sourceOrderInfo").find("iframe").contents().find(".window-shadow").length<1){
			$("#sourceOrderInfo").find("iframe")[0].contentWindow.$.messager.progress();
			$("#sourceOrderInfo").find("iframe").contents().find(".window-shadow").hide();
			$("#sourceOrderInfo").find("iframe").contents().find(".messager-window").hide();
		}
		$("#applyOrderInfo").find("iframe").contents().find("#printArea").siblings().hide();
		$("#applyOrderInfo").find("iframe").contents().find("#lock").show();
		$("#applyOrderInfo").find("iframe").contents().find("#unlock").show();
		$("#applyOrderInfo").find("iframe").contents().find("#menubutton_index").show();

	},//修改订单提示修改类型及记录修改内容
	initDialog:function(){
		return;
		var str_content=
			'<div></div>'
		$('div#easyui_dialog_apply').dialog({
            modal: true,
            title: '订单修改申请',
            width: "300",
            height: "200",
            closed: false,
            border:false,
            cache: false,
            content:str_content,
            onClose : function() {  
               $("div#easyui_dialog_index").dialog('destroy');  
               $("body").append("<div id='easyui_dialog_apply'></div>");
            }
        });
	},
	destroydialog:function(){
        $('div#easyui_dialog_apply').dialog("close");
    },
    sysAnalysisOprate:function(){
    	var sourceOrderInfoLength = $("#sourceOrderInfo").find("iframe").contents().find("[name]").length;
		var applyOrderInfoLength  = $("#applyOrderInfo").find("iframe").contents().find("[name]").length;
    	
    },
    //订单保存前提供时间保存图片
    applySubmitBase64:function(){
    	$("#applySubmit").hide();
    	//$("#applyOrderInfo").find("iframe")[0].contentWindow.menubuttonIndexTool.saveAsBase64img();
    	$.messager.progress({title:'请等待',msg:'<span style="color:green">订单图片自动上传中...</span>'});
    	setTimeout(function(){
    		$.messager.progress('close');
    		page_orderApplyAudit.applySubmit();
    		$("#applySubmit").show();
    	},2000)
    },
    //订单申请提交
    applySubmit:function(){
        $.messager.progress({title:'请等待',msg:'<span style="color:green">订单模版提交中...</span>'});
    	var html_info = $("#applyOrderInfo").find("iframe")[0].contentWindow.document.documentElement.outerHTML;
    	var data = $("#applyOrderInfo").find("iframe")[0].contentWindow.basic_index.getSaveTemplateInfoData();
    	var order_s_table_source_arr= data["order_s_table_source_arr"];
    	var table_source_line_total = data["table_source_line_total"];
        var order_s_table_source_arr_str=data["order_s_table_source_arr_str"];
        var order_s_table_source_arr_for_two= data["order_s_table_source_arr_for_two"];
    	var table_source_line_total_for_two = data["table_source_line_total_for_two"];
        var order_s_table_source_arr_str_for_two=data["order_s_table_source_arr_str_for_two"];
        var check = $("#applyOrderInfo").find("iframe")[0].contentWindow.basic_index.checkmyformsubmit();
        if(!check){
        	return;
        }
        //保存模版
        $.ajax({
            type: 'POST',
            url: serverUrl+"/createOrderApply/saveAsOrderTemplate",
            data: {"apply_id":page_orderApplyAudit.apply_id,"type":1,"temp_name":"sysapptemp","temp_info":html_info.replace('document.write(localStorage.getItem("temp_info_select"))',''),"order_s_table_source_arr":order_s_table_source_arr,"table_source_line_total":table_source_line_total,"order_s_table_source_arr_str":order_s_table_source_arr_str
            	,"order_s_table_source_arr_for_two":order_s_table_source_arr_for_two,"table_source_line_total_for_two":table_source_line_total_for_two,"order_s_table_source_arr_str_for_two":order_s_table_source_arr_str_for_two},
            success: function(data){
              $.messager.progress('close');
              if(data.result){
                  var template_id=data.data.id;
                  var check = $("#applyOrderInfo").find("iframe")[0].contentWindow.basic_index.checkmyformsubmit();
                  if(check){//订单校验完成 保存订单
                      $.messager.progress({title:'请等待',msg:'<span style="color:green">订单数据提交中...</span>'});
                  	  var data = $("#applyOrderInfo").find("iframe")[0].contentWindow.myformUtils.formToArrByAllNames("#myformsubmit","order_");
                      data["apply_id"]=page_orderApplyAudit.apply_id;
                      //$("#applyOrderInfo").find("iframe")[0].contentWindow.menubuttonIndexTool.saveAsBase64img();
                  	  $.ajax({
                        type: 'POST',
                        url: serverUrl+"/createOrderApply/saveOrderInfo",
                        data: data,
                        success: function(data){
                          $.messager.progress('close');
                          if(data.result){
                              //写入申请apply
                              page_orderApplyAudit.saveAsApplyInfo();
                          }else{
                              $.messager.alert('提示','模版订单保存失败!'+data.info,'info');
                          }
                        }
                      }); 
                  }
              }else{
                  $.messager.alert('提示','保存失败!','info');
              }
            }
          }); 
    },//写入申请单
    saveAsApplyInfo:function(){
        $.messager.progress({title:'请等待',msg:'<span style="color:green">订单申请表单提交中...</span>'});
    	$.ajax({
            type: 'POST',
            url: serverUrl+"/createOrderApply/saveAsApplyInfo",
            data: {
            	"apply_id":page_orderApplyAudit.apply_id,
            	"type":1,
            	"remark":$("#myremark").text(),
            	"special_remark":page_orderApplyAudit.checkAuditSubmit(),
            	"order_no":page_orderApplyAudit.order_no,
            	"order_p_product_no":page_orderApplyAudit.order_p_product_no
            },
            success: function(data){
            	$.messager.progress('close');
            	if(data.result){
            		alert('申请提交成功!');
            		$("#min_title_list",parent.document).find('[data-href="orderlist.html"]').click();
            		//延迟关闭
            		$.messager.progress({title:'请等待',msg:'<span style="color:green">申请单即将关闭...</span>'});
            		var time = 3;
					setTime = setInterval(function() {
						if (time <= 0) {
							clearInterval(setTime);
							$("#min_title_list",parent.document).find('[data-href="./html/createOrder/orderApplyMainForApply.html"]').next().click();
							return;
						}
						time--;
					}, 1000);
            	}else{
            		alert('申请失败!');
            	}
            }
    	});
    },
    checkAuditSubmit:function(){
    	//source_factory_colresizeable_for_two??
    	var source = $("#sourceOrderInfo").find("iframe").contents().find(".source_factory_colresizeable");
    	var apply  = $("#applyOrderInfo").find("iframe").contents().find(".source_factory_colresizeable");
    	var str="";
    	var str_data="";
    	//样式5原料除去大组件
    	$("#sourceOrderInfo").find("iframe").contents().find("[id^='draggable_source_factory_content_']").each(function(index,el){
    		if($(this).find(".source_factory_colresizeable").length<1){
    			var id_input=$(this).attr("id");
    			//查找input 原材料整条数据
    			var val_1=$(this).find("[name]").map(function(){
    				  return $(this).val();
    			}).get().join(", ");
    			//查找input 原材料整条数据 申请单中
    			var val_2=$("#applyOrderInfo").find("iframe").contents().find("[id='"+id_input+"']").find("[name]").map(function(){
    				  return $(this).val();
    			}).get().join(", ");
    			//必须投料数大于0才能算为需要退回
    			if(val_1!=val_2&&$(this).find("[name='order_s_input_val_feed_total']").val()>0){
    				str_data+="-1&"+$(this).find("[name='order_s_input_material_type']").text()+"&"
    						+$(this).find("[name='order_s_input_material_house']").val()+"|"
    				str+="原料仓 样式5 物料"+$(this).find("[name='order_s_input_material_type']").text()+"有修改\r\n"
    			}
    		}
    	});
    	//辅料样式5
    	$("#sourceOrderInfo").find("iframe").contents().find("[id^='draggable_second_factory_content_']").each(function(index,el){
   			var id_input=$(this).attr("id");
   			//查找input 原材料整条数据
   			var val_1=$(this).find("[name]").map(function(){
   				  return $(this).val();
   			}).get().join(", ");
   			//查找input 原材料整条数据 申请单中
   			var val_2=$("#applyOrderInfo").find("iframe").contents().find("[id='"+id_input+"']").find("[name]").map(function(){
   				  return $(this).val();
   			}).get().join(", ");
   			//必须投料数大于0才能算为需要退回
   			if(val_1!=val_2&&$(this).find("[name='order_s_input_val_feed_total']").val()>0){
   				str_data+="-1&"+$(this).find("[name='order_s_input_material_type']").text()+"&"
   						+$(this).find("[name='order_s_input_material_house']").val()+"|"
   				str+="辅料仓 样式5 物料"+$(this).find("[name='order_s_input_material_type']").text()+"有修改\r\n"
   			}
    	});
    	//避免和大组件冲突
    	if(str.length>0){alert(str)};
    	str="";
    	//样式二 三    	
    	//所有都要判断的
    	//判断所有行 原行数大于现行数 则所有物料判断
    	if($(source).find("[name='order_s_line_no']").length>$(apply).find("[name='order_s_line_no']").length){
    		str+="原行数大于现行数 则所有物料判断\r\n";
    		for (var i = 0; i < $(source).find("[name='order_s_line_no']").length; i++) {
    			str_data+=(i+1)+"&all&all|";
			}
    	}
    	if(str.length>0){alert(str);return str_data;}
    	
    	//判断每行大的订单数和投料数
    	var feed_total_source=$(source).find("[name='order_s_feed_total']").map(function(){return $(this).text();}).get();
    	var feed_total_apply=$(apply).find("[name='order_s_feed_total']").map(function(){return $(this).text();}).get(); 
    	for(i=0;i<feed_total_source.length;i++){
    		if(feed_total_source[i]>feed_total_apply[i]){
    			str+=(i+1)+",";
    			str_data+=(i+1)+"&all&all|";
    		}
    	}
    	if(str.length>0){alert(str+"行 投料数有修改 则所有物料判断\r\n");return str_data;}
    	
    	var order_total_source=$(source).find("[name='order_s_order_total']").map(function(){return $(this).text();}).get();
    	var order_total_apply=$(apply).find("[name='order_s_order_total']").map(function(){return $(this).text();}).get(); 
    	for(i=0;i<order_total_source.length;i++){
    		if(order_total_source[i]>order_total_apply[i]){
    			str+=(i+1)+",";
    			str_data+=(i+1)+"&all&all|";
    		}
    	}
    	if(str.length>0){alert(str+"行 订单数有修改 则所有物料判断\r\n");return str_data;}
    	
    	//对比物料 order_s_material_type
    	var order_s_material_type_source=$(source).find("[name='order_s_material_type']").map(function(){return $(this).text();}).get();
    	var order_s_material_type_apply=$(apply).find("[name='order_s_material_type']").map(function(){return $(this).text();}).get().join(","); 
    	for(i=0;i<order_s_material_type_source.length;i++){
    		if(order_s_material_type_apply.indexOf(order_s_material_type_source[i])<0){
    			str+=order_s_material_type_source[i]+"&"+$(source).find("[name='order_s_material_house']").eq(i).val()+",";
    			str_data+="all&"+order_s_material_type_source[i]+"&"+$(source).find("[name='order_s_material_house']").eq(i).val()+"|";
    		}
    	}
    	if(str.length>0){alert(str+" 被删除");return str_data;}
    	//对比子列 
    	var table_source_arr_source_arr=$("#sourceOrderInfo").find("iframe")[0].contentWindow.tbsourcefactoryColresizeEvent.table_source_arr;
    	var order_s_material_type_source=$(source).find("[name='order_s_material_type']").map(function(){return $(this).text();}).get();
    	var table_source_arr_apply_arr=$("#applyOrderInfo").find("iframe")[0].contentWindow.tbsourcefactoryColresizeEvent.table_source_arr;
    	for(i=0;i<table_source_arr_source_arr.length;i++){
    		if(order_s_material_type_source[i]=="订单数"||order_s_material_type_source[i]=="投料数"){
    			continue;
    		}
    		if(table_source_arr_source_arr[i]!=table_source_arr_apply_arr[i]){
    			str+=order_s_material_type_source[i]+"|"+$(source).find("[name='order_s_material_house']").eq(i).val()+",";
    			str_data+="all&"+order_s_material_type_source[i]+"&"+$(source).find("[name='order_s_material_house']").eq(i).val()+"|"
    		}
    	}
    	if(str.length>0){alert(str+" 子列有变更");return str_data;}
    	//对比数据变化
    	$(source).find("tr").each(function(index,el){
    		if(index>2){//第三行数据行开始对比
    			$(el).find("[name]").each(function(i,el_el){
    				var name = $(el_el).attr("name");
    				if($(el_el).text().trim()!=$(apply).find("tr").eq(index).find("[name]").eq(i).text().trim()){
    					var uniqueid = $(el_el).parent().attr("uniqueid");
    					var order_s_material_type = $(source).find("[uniqueid='"+uniqueid+"']").find("[name='order_s_material_type']").text();
    					var order_s_material_house = $(source).find("[uniqueid='"+uniqueid+"']").find("[name='order_s_material_house']").val();
    	    			str+=(index-2)+"|"+order_s_material_type+"|"+order_s_material_house+",";
    	    			str_data+=(index-2)+"&"+order_s_material_type+"&"+order_s_material_house+"|";
    				}
    			})
    		}
    	})
    	if(str.length>0){alert(str+" 数据有变更");return str_data;}
    	return str_data;
    },//查找不同
    finddiff:function(){
    	var json={};
    	$("#sourceOrderInfo").find("iframe").contents().find("#myformsubmit").find("[name^='order_']").each(function(index,el){
    		var name=$(this).attr("name");
    		if(json[name]!=undefined){
    			json[name]=parseInt(json[name])+1;
    		}else{
    			json[name]=0;
    		}
    		var applyOrderInfoDom=$("#applyOrderInfo").find("iframe").contents().find("#myformsubmit").find("[name='"+name+"']").eq(json[name]);
			if($(applyOrderInfoDom).val()!=undefined&&$(this).text().trim()!=$(applyOrderInfoDom).text().trim()){
				$(applyOrderInfoDom).css({"background-color":"#ffeb3b"})
				return;
    		}
			if($(applyOrderInfoDom).val()!=undefined){
				if($(applyOrderInfoDom).val()!=undefined&&$(this).val().trim()!=$(applyOrderInfoDom).val().trim()){
	    			$(applyOrderInfoDom).css({"background-color":"#ffeb3b"})
	    			return;
	    		}
			}
			$(applyOrderInfoDom).css({"background-color":"white"})
    	});
    }
}	
setInterval(() => {
	page_orderApplyAudit.finddiff();
}, 1000);
page_orderApplyAudit.init();
</script>
</body>
</html>